<template>
  <section class="righttask-box">
    <div class="corner-lb"></div>
    <div class="corner-rb"></div>
    <h3 class="task-title">
      在线设备
      <img class="icon" src="../../../../../assets/leftbg10.png" alt="" />
    </h3>
    <div class="task-num">
      <div class="task-online">
        <span>当前在线</span>
        <span class="task-size">{{ onlineList.length }}</span>
      </div>
      <section style="height: 38vh;overflow-y: auto;">
        <div class="task-list" :class="{
          'task-list-active': selectedItem && selectedItem.id === item.id,
        }" v-for="(item, index) in onlineList" :key="index">
          <!-- @click="handleClick(item)" -->
          <div class="task-con">
            <div class="task-icon-box">
              <img class="task-icon" src="../../../../../assets/rightbg10.png" alt="" />
            </div>
            <div class="task-redress">
              <p class="p1">{{ item.airportName }}</p>
              <p class="task-margintop">{{ item.description }}</p>
            </div>
          </div>
          <div class="task-bcon">
            <p class="lefttask-con-tit">
              {{
                (item.flightStatus || item.flightStatus === 0) ? getStatusName(item.flightStatus) : "离线"
              }}
            </p>
          </div>
        </div>
      </section>
    </div>
  </section>
</template>
<script lang="ts" setup>
import { ref, onMounted, defineProps, watch } from "vue";
import { ElScrollbar } from "element-plus";

// 定义机场项目的类型
interface AirportItem {
  id: string | number;
  airportName: string;
  description: string;
  status: string;
}

const props = defineProps({
  onlineList: {
    type: Array as () => AirportItem[],
    default: () => [],
  },
});

const emit = defineEmits<{
  handleClick: [item: AirportItem | null];
}>();

const statusList = ref([
  {
    status: "0",
    statusName: "空闲",
  },
  {
    status: "1",
    statusName: "航线任务中",
  },

  {
    status: "2",
    statusName: "手动飞行中",
  },
]);
const getStatusName = (status: string) => {
  return statusList.value.find((item) => item.status == status)?.statusName;
};

// 选中的项目
const selectedItem = ref<AirportItem | null>(null);

// 处理点击事件
const handleClick = (item: AirportItem) => {
  // 如果点击的是已选中的项目，则取消选中
  if (selectedItem.value && selectedItem.value.id === item.id) {
    selectedItem.value = null;
  } else {
    // 否则选中新项目
    selectedItem.value = item;
  }

  // 向父组件发送事件
  emit("handleClick", selectedItem.value);
};

// 监听 onlineList 变化，重置选中状态
// watch(
//   () => props.onlineList,
//   () => {
//     selectedItem.value = null;
//   }
// );
</script>

<style lang="scss" scoped>
.righttask-box {
  background: rgb(2 19 20 / 80%);
  border: 1px solid;
  margin-top: 0.5vh;
  border-image: radial-gradient(circle,
      rgb(36 229 241 / 17%),
      rgb(255 255 255 / 17%)) 1 1;
  height: 50vh;
  position: relative;

  &::before {
    content: "";
    position: absolute;
    top: -1px;
    left: 0px;
    width: 22px;
    height: 22px;
    border-top: 1px solid #24e5f1;
    opacity: 0.4;
  }

  &::after {
    content: "";
    position: absolute;
    top: -1px;
    right: -1px;
    width: 22px;
    height: 22px;
    border-top: 1px solid #24e5f1;
    opacity: 0.4;
  }

  .corner-lb {
    position: absolute;
    bottom: -1px;
    left: -1px;
    width: 22px;
    height: 22px;
    border-bottom: 1px solid #24e5f1;
    border-left: 1px solid #24e5f1;
    opacity: 0.4;
  }

  .corner-rb {
    position: absolute;
    bottom: -1px;
    right: -1px;
    width: 22px;
    height: 22px;
    border-bottom: 1px solid #24e5f1;
    border-right: 1px solid #24e5f1;
    opacity: 0.4;
  }

  .task-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 4vh;
    padding-left: 1.5vh;
    margin: 0;
    font-family: PingFangSC, "PingFang SC";
    font-size: 2vh;
    font-weight: 500;
    color: #fffefe;
    user-select: none;
    background: radial-gradient(ellipse 140% 300% at 100% 50%,
        rgba(1, 34, 35, 0.2) 28%,
        rgba(7, 217, 221, 0.7) 100%);

    .icon {
      width: 3vh;
      margin-right: 1vw;
    }
  }
}

.task-num {
  padding: 1vh;
  // border: 1px solid #eee;

  .task-online {
    display: flex;
    align-items: center;
    font-size: 1.8vh;
    font-weight: 400;

    .task-size {
      margin-left: 1vw;
      font-size: 3.8vh;
      font-weight: bold;
      color: #eebf1b;
    }
  }

  .task-list {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 1.5vh;
    margin-top: 1vh;
    background: radial-gradient(ellipse 140% 300% at 100% 50%,
        rgba(1, 34, 35, 0.2) 28%,
        rgba(5, 73, 75, 0.7) 100%);
    border-radius: 0.5vw;
    cursor: pointer;

    .task-con {
      display: flex;
      width: calc(100% - 10vh);

      .task-icon-box {
        width: 8vh;

        .task-icon {
          width: 8vh;
          height: 7vh;
        }
      }

      .task-redress {
        margin-left: 2vh;
        width: calc(100% - 10vh);

        .p1 {
          font-size: 1.5vh;
          font-weight: 600;
          max-height: 5vh;
          display: -webkit-box;
          -webkit-line-clamp: 3;
          -webkit-box-orient: vertical;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: normal;
          word-break: break-all;
        }


        .task-margintop {
          margin-top: 2vh;
          font-size: 1.5vh;
          font-weight: 400;
        }
      }
    }

    .task-bcon {
      width: 10vh;
    }

  }

  .lefttask-con-tit {
    height: 4.5vh;
    font-size: 1.6vh;
    font-weight: 400;
    line-height: 4.5vh;
    text-align: center;
    // background: #00b0bb;
    border-radius: 3vw;
    // border: 1px solid transparent;
    box-sizing: border-box;
  }
}

.task-list-active {
  box-sizing: border-box;
  border: 1px solid #00adb8;
}

// 隐藏 ElScrollbar 的滚动条
:deep(.el-scrollbar__bar) {
  opacity: 0;
  transition: opacity 0.3s;
}

:deep(.el-scrollbar__bar.is-horizontal) {
  height: 0;
}

:deep(.el-scrollbar__bar.is-vertical) {
  width: 0;
}

// 可选：鼠标悬停时显示滚动条
:deep(.el-scrollbar:hover .el-scrollbar__bar) {
  opacity: 0.3;
}
</style>
